/**
 * 
 */
package com.dalonedrow.engine.sprite.base;

import java.io.BufferedReader;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.dalonedrow.pooled.PooledStringBuilder;
import com.dalonedrow.pooled.StringBuilderPool;

/**
 * @author Donald
 */
public final class FontProperties {
	/** the one and only instance of the <code>SpriteLibrary</code> class. */
	private static FontProperties	instance;
	/**
	 * Gives access to the singleton instance of {@link FontProperties}.
	 * @param folder the name of the assets folder
	 * @return {@link FontProperties}
	 */
	public static FontProperties getInstance(final String folder) {
		if (FontProperties.instance == null) {
			FontProperties.instance = new FontProperties();
		}
		instance.libraryFolder = folder;
		return FontProperties.instance;
	}
	/**
	 * Gives access to the singleton instance of {@link FontProperties}.
	 * @return {@link FontProperties}
	 */
	public static FontProperties getInstance() {
		if (FontProperties.instance == null) {
			FontProperties.instance = new FontProperties();
		}
		return FontProperties.instance;
	}
	/** the folder containing the image library. */
	private String						libraryFolder;
	/** the map of all defined font properties. */
	private HashMap<String, HashMap<Character, SimpleDimension>>	fontProperties;
	/**
	 * Hidden constructor.
	 */
	private FontProperties() {
		fontProperties	=
				new HashMap<String, HashMap<Character, SimpleDimension>>();
	}
	public HashMap<Character, SimpleDimension> getCharacterMap(
			final String fontName,
			final int fontSize) {
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(fontName);
		sb.append("_");
		sb.append(fontSize);
		HashMap<Character, SimpleDimension> map =
				fontProperties.get(sb.toString());
		StringBuilderPool.getInstance().returnObject(sb);
		return map;
	}
	/**
	 * Initializes the UI.
	 * @param propertyFile the name of the properties file
	 * @throws Exception if there is an error loading values
	 */
	public void initFontProperties(final String propertyFile)
			throws Exception {
		PooledStringBuilder fileName =
				StringBuilderPool.getInstance().getStringBuilder();
		fileName.append(libraryFolder);
		fileName.append("/");
		fileName.append(propertyFile);
		FileHandle file = Gdx.files.internal(fileName.toString());
		fileName.returnToPool();
		fileName = null;
		final int defaultCharBufferSize = 8192;
		BufferedReader input = file.reader(defaultCharBufferSize);
		String inText = input.readLine(); // priming read
		// as long as lines are being read from file
		HashMap<Character, SimpleDimension> map =
				new HashMap<Character, SimpleDimension>();
		while (inText != null) {
			if (inText.length() != 0) {
				char c = inText.charAt(0);
				SimpleDimension d = new SimpleDimension(
						Integer.parseInt(inText.substring(2).split(",")[0]),
						Integer.parseInt(inText.substring(2).split(",")[1])
						);
				map.put(c, d);
			}
			inText = input.readLine(); // read next line
		}
		input.close(); // not necessary, but still good practice
		if (!map.isEmpty()) {
			fontProperties.put(
					propertyFile.split(".properties")[0],
					map
					);
		}
	}
}
